/**
 * Writed by yura and mak
 */

package wz.legend.handlers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import wz.legend.Context;
import wz.legend.HttpContext;


public class DumpHandler implements ResponseHandler {
    private static final String ENCODING = "utf-8";

    /**
     * Logger for this class
     */
    private static final Logger LOG = Logger.getLogger(DumpHandler.class);

    private boolean toFile;
    private String filePath;
    private boolean withHeader;

    private String contentType;

    public boolean isToFile() {
        return toFile;
    }

    public void setToFile(boolean toFile) {
        this.toFile = toFile;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public void handle(HttpContext httpContext, Context context) {
        HttpEntity entity = httpContext.getResponse().getEntity();
        FileOutputStream file = null;
        try {
            if ((entity.getContentType() != null && entity.getContentType()
                    .getValue().indexOf(getContentType()) != -1)
                    || getContentType().equals("*")) {
                String content = EntityUtils.toString(entity, ENCODING);
                if (toFile) {
                    File dir = new File(filePath);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    file = new FileOutputStream(filePath
                            + File.separator
                            + System.currentTimeMillis()
                            + " "
                            + URLEncoder.encode(httpContext.getRequest()
                                    .getRequestLine().getUri(), ENCODING));
                    if (withHeader) {
                        IOUtils.writeLines(Arrays.asList(httpContext
                                .getResponse().getAllHeaders()), "\r\n", file);
                        IOUtils.write("\r\n", file);
                    }
                    IOUtils.write(content, file);
                } else {
                    LOG.debug(httpContext.getRequest().getRequestLine() + "\n");
                    LOG.debug(content);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(file != null)
                try {
                    file.close();
                } catch (IOException e) {
                }
        }
                   
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public boolean isWithHeader() {
        return withHeader;
    }

    public void setWithHeader(boolean withHeader) {
        this.withHeader = withHeader;
    }
}
